from flask import Flask, render_template, redirect, url_for
from werkzeug.routing import BaseConverter

app = Flask(__name__)

"""
当在路由后面添加参数的时候(查询单个，删除等接口)，django里面有默认的正则匹配，flask中没有，我们可以自定义
"""


class RegexConverter(BaseConverter):
    """
    自定义URL匹配正则表达式
    """

    def __init__(self, map, regex):
        super(RegexConverter, self).__init__(map)
        self.regex = regex

    def to_python(self, value):
        """
        路由匹配时，匹配成功后传递给视图函数中参数的值
        :param value:
        :return:
        """
        return int(value)

    def to_url(self, value):
        """
        使用url_for反向生成URL时，传递的参数经过该方法处理，返回的值用于生成URL中的参数
        :param value:
        :return:
        """
        val = super(RegexConverter, self).to_url(value)
        return val


app.url_map.converters['xxx'] = RegexConverter


@app.route('/index/<xxx("\d+"):nid>', methods=['GET', 'POST'])
def index(nid):
    print(nid, type(nid))
    v = url_for('index', nid=999)  # /index/999
    print(v)
    return "Index"


if __name__ == '__main__':
    app.run()
